package ar.edu.itba.it.paw.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;


import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;


@Entity
@Table(name="SystemUser")
public class User extends PersistentAttributes {
	
	@Column(unique = true)
	private String username;
	private String password;
	private String first_name;
	private String last_name;
	private Date last_login;
	@Column(unique = true)
	private String mail;
	private boolean admin;
	@OneToMany(cascade=CascadeType.ALL, mappedBy = "owner")
	private Set<Product> products_set;
	@OneToMany(cascade=CascadeType.ALL, mappedBy = "seller")
	private Set<Transaction> sold;
	@OneToMany(cascade=CascadeType.ALL, mappedBy = "buyer")
	private Set<Transaction> bought;
	@OneToMany(cascade=CascadeType.ALL, mappedBy = "user")
	private Set<Offer> offers_set;
	
	

	protected User(){

	}
	
	//This constructor is used to create users before they are inserted to the database
	public User(String username, String password, String first_name, String last_name, String mail) throws IllegalArgumentException{
				this.setUsername(username);
				this.setPassword(password);
				this.setFirst_name(first_name);
				this.setLast_name(last_name);
				this.setMail(mail);
	}
	
	private void setUsername(String username) {
		if(username != null && !username.equals("") && username.length() <= 20)
			this.username = username;
		else
			throw new IllegalArgumentException();
	}

	public String getUsername() {
		return username;
	}

	public void setPassword(String password) {
		if(password != null & !password.equals("") && password.length() >= 8)
			this.password = password;
		else
			throw new IllegalArgumentException();
	}

	public String getPassword() {
		return password;
	}

	private void setMail(String mail) {
		if(mail != null && !mail.equals(""))
			this.mail = mail;
		else
			throw new IllegalArgumentException();
	}

	public String getMail() {
		return mail;
	}
	
	public String getFirst_name() {
		return first_name;
	}
	
	public void setFirst_name(String first_name) {
		if(first_name != null && !first_name.equals(""))
			this.first_name = first_name;
		else
			throw new IllegalArgumentException();
	}
	
	public String getLast_name() {
		return last_name;
	}
	
	public void setLast_name(String last_name) {
		if(last_name != null && !last_name.equals(""))
			this.last_name = last_name;
		else
			throw new IllegalArgumentException();
	}

	public Set<Product> getProducts_set() {
		Set<Product> set = new HashSet<Product>();
		for(Product prod: products_set){
			if(prod.getQuantity() > 0){
				set.add(prod);
			}
		}
		return set;
	}

	public ArrayList<Auction> getPendingAuctions(){
		ArrayList<Auction> auctions = new ArrayList<Auction>();
		
		for (Iterator<Product> iter = products_set.iterator(); iter.hasNext();) {
			Product elem = (Product)iter.next();
			if(elem.getClass() == Auction.class && !((Auction)elem).isEnded() && ((Auction)elem).hasOffers()){
				auctions.add((Auction)elem);
			}
		}
		return auctions;
	}
	
	public Set<Transaction> getSold() {
		return sold;
	}

	public Set<Transaction> getBought() {
		return bought;
	}

	public boolean isAdmin() {
		return admin;
	}

	public Set<Offer> getOffers_set() {
		return offers_set;
	}

	public void setLast_login(Date last_login) {
		
		this.last_login = last_login;
	}

	public Date getLast_login() {
		return last_login;
	}

	public List<Question> getUnansweredQuestions() {
		List<Question> questions = new ArrayList<Question>();
		for(Product prod : getProducts_set()){
			for(Question question : prod.getQuestions()){
				if(question.getAnswer() == null)
				questions.add(question);
			}
		}
		return questions;
	}
	
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password
				+ ", nombre=" + first_name + ", apellido=" + last_name + ", mail="
				+ mail + ", userId=" + getId() + "]";
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((mail == null) ? 0 : mail.hashCode());
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (mail == null) {
			if (other.mail != null)
				return false;
		} else if (!mail.equals(other.mail))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}

}
